from Conv.model import baseline_conv,linear_conv_test
import h5py
from skimage import io, util
import matplotlib.pyplot as plt
import numpy as np
import keras.backend as K
import json

def show_weights_distribution():
    weights_path = '/home/dell/wxm/Code/Denosing/Conv/metadata/darken_weights_mse_bl.h5'
    f = h5py.File(weights_path)
    print f
    for k in range(f.attrs['nb_layers']):
        g = f['layer_{}'.format(k)]
        weights = g['param_0']
        plt.hist(np.reshape(weights.value,(np.prod(weights.value.shape),1)),bins=100)
        plt.show()

def show_feature_map():
    model = linear_conv_test(1,512,512)
    model.load_weights('/home/dell/wxm/Code/Denosing/Conv/metadata/darken_weights_mse_lc.h5')
    img_arr = io.imread('/media/dell/cb552bf1-c649-4cca-8aca-3c24afca817b/dell/wxm/Data/decsai/GM512/inf/Town.pgm')
    img_arr_scale = np.reshape(img_arr/255.,(1,1,img_arr.shape[0],img_arr.shape[1]))
    """
    weights_path = '/home/dell/wxm/Code/Denosing/Conv/metadata/darken_weights_mse_bl.h5'
    f = h5py.File(weights_path)
    nb_img = 9
    print f
    for k in range(f.attrs['nb_layers']):
        g = f['layer_{}'.format(k)]
        weights = g['param_0']
        for i in range(nb_img):
            i += 1
        plt.show()
    """
    print len(model.layers)
    middel_re = K.eval(model.layers[0](K.variable(img_arr_scale)))
    print middel_re.shape
    middel_re = middel_re * 255
    middel_re = np.reshape(middel_re,(middel_re.shape[2],middel_re.shape[3]))
    middel_re = util.img_as_ubyte(np.asarray(middel_re,dtype=np.uint8))
    print middel_re.shape
    io.imshow(middel_re)
    io.show()

def show_loss_curve():
    json_file = '/home/dell/wxm/Code/Denosing/Conv/metadata/darken_training_mse_bl.json'
    f = open(json_file,'r')
    j = json.load(f)

    loss = j['loss']
    val_loss = j['val_loss']
    curve = np.asarray([loss,val_loss])
    print curve.shape
    plt.plot(curve.transpose())
    plt.show()

if __name__ == '__main__':
    show_loss_curve()